/* -------------------------------------------------------------------------- *
   WinUnit - Maria Blees (maria.blees@microsoft.com)
 * -------------------------------------------------------------------------- */
///  @file PlainTextLogger.cpp

#include "PlainTextLogger.h"

namespace WinUnitLib
{
    PlainTextLogger::PlainTextLogger()
        :
    _verbosity(0)
    {
    }

    void PlainTextLogger::OutputTestExecutableNamePreTest(
        const wchar_t* /* fullPath */, 
        const wchar_t* nameOfExecutable)
    {
        if (_verbosity > 0)
        {
            *this << L"Processing [" << nameOfExecutable << L"]...\n";
        }
    }

    void PlainTextLogger::OutputTestExecutableNamePostTest(
        const wchar_t* /* fullPath */, 
        const wchar_t* nameOfExecutable, 
        unsigned int succeeded, 
        unsigned int totalRun)
    {
        if (totalRun == 0)
        {
            *this << L"[" << nameOfExecutable << L"] " << 
                L"No tests were run.\n";
            return;
        }

        wchar_t* successString = (succeeded == totalRun) ? L"SUCCEEDED" : L"FAILED";
        if (succeeded != totalRun || _verbosity > 0)
        {
            *this << L"[" << nameOfExecutable << L"] " << successString 
                  << L".  Tests run: " << totalRun << L"; Failures: " 
                  << (totalRun - succeeded) << L".\n"; 
        }
    }

    void PlainTextLogger::OutputTestNamePreTest(const char* testName)
    {
        if (_verbosity > 0)
        {
            *this << L"(" << testName << L")" << L"\n";
        }
    }

    void PlainTextLogger::OutputTestNamePostTest(
        const char* testName, 
        bool passed)
    {
        wchar_t* successString = passed ? L"SUCCEEDED" : L"FAILED";
        if (_verbosity > 1 || !passed)
        {
            *this << successString << L": " << testName << L".\n";
        }
    }

    void PlainTextLogger::OutputTrace(const wchar_t* message)
    {
        if (_verbosity > 0)
        {
            *this << message;
        }
    }

    void PlainTextLogger::OutputTestError(const wchar_t* message)
    {
        *this << message << L"\n";
    }

    void PlainTextLogger::OutputProgramErrorLine(
        const wchar_t* msg1,
        const wchar_t* msg2,
        const wchar_t* msg3,
        const wchar_t* msg4)
    {
        *this << L"[WinUnit] ";

        if (msg1) { *this << msg1; }
        if (msg2) { *this << msg2; }
        if (msg3) { *this << msg3; }
        if (msg4) { *this << msg4; }

        *this << L"\n";
    }

    void PlainTextLogger::OutputFinalResults(
        unsigned int succeeded, 
        unsigned int totalRun)
    {
        *this << L"\n";
        if (totalRun == 0)
        {
            *this << L"No tests were found.\n";
            return;
        }
        else if (succeeded == totalRun)
        {
            *this << L"All tests passed.\n";
        }
        else
        {
            *this << L"There were errors.\n";
        }
        *this << L"Tests run: " << totalRun 
            << L"; Failed: " << (totalRun - succeeded) << L".\n";
    }

    void PlainTextLogger::SetVerbosity(unsigned int verbosity)
    {
        _verbosity = verbosity;
    }
}